package com.baiyang.server.service.read.impl;


import com.alibaba.fastjson.JSONObject;
import com.baiyang.server.model.article.Article;
import com.baiyang.server.model.system.Author;
import com.baiyang.server.model.system.Resource;
import com.baiyang.server.service.article.ArticleService;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.system.ResourceService;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.read.BookshelfDao;
import com.baiyang.server.model.read.Bookshelf;
import com.baiyang.server.service.read.BookshelfService;

import java.util.List;

/**
 * 
 * @author XVX
 * BY: @Primary 注解帮助生命@Autowired优先注入的实列
 */
@Service	
public class BookshelfServiceImpl extends BaseServiceImpl<BookshelfDao, Bookshelf> implements BookshelfService{

    @Autowired
    private ArticleService articleService;
    @Autowired
    private BookshelfDao bookshelfDao;
    @Autowired
    private ResourceService resourceService = null;

    /**
     * 获取关注状态
     * @param readId
     * @param articleId
     * @return
     */
    @Override
    public Boolean getFollowState(String readId, String articleId) {
        // 获取作者ID
        Article article = new Article();
        article.setId(articleId);
        article = articleService.selectOne(article);
        if(article == null){
            throw new NullPointerException();
        }else{
            Bookshelf bookshelf = new Bookshelf();
            bookshelf.setAuthorId(article.getAuthorId());
            bookshelf.setUserId(readId);
            bookshelf.setDel("0");
            bookshelf = bookshelfDao.selectOne(bookshelf);
            if(bookshelf == null){
                return false;
            }else{
                return true;
            }
        }
    }

    /**
     * 关注
     * @param articleId
     * @param readId
     */
    @Override
    public void follow(String articleId, String readId) {
        // 获取作者ID
        Article article = new Article();
        article.setId(articleId);
        article = articleService.selectOne(article);
        if(article == null){
            throw new NullPointerException();
        }else{
            Bookshelf bookshelf = new Bookshelf();
            bookshelf.setAuthorId(article.getAuthorId());
            bookshelf.setUserId(readId);
            bookshelf.setDel("0");
            bookshelf.setId(DataTool.createUUID());
            bookshelfDao.insert(bookshelf);
        }
    }

    /**
     * 获取关注列表
     * @param readId
     * @return
     */
    @Override
    public List<Author> getFollowList(String readId) {
        List<Author> authors = bookshelfDao.getFollowList(readId);
        authors.forEach(e->{
            // 获取最新作品信息
            Article article = new Article();
            article.setAuthorId(e.getId());
            article = articleService.getNewArticle(article);
            if(article != null){
                e.setLastArticle(article.getArticleName());
                e.setLastTime(article.getCreateTime());
            }
            // 获取头像信息
            Resource resourcePic = new Resource();
            resourcePic.setCreateBy("auto");
            resourcePic.setRelationId(e.getId());
            resourcePic.setRelationType("AVATAR");
            resourcePic.setType("PIC");
            resourcePic = resourceService.selectOne(resourcePic);
            if(resourcePic != null){
                e.setAvatarUrl(resourcePic.getPath());
            }
        });
        return authors;
    }
}
